package com.android.svmc.app.mobile.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.svmc.app.mobile.activity.R;
import com.android.svmc.app.mobile.data.DownloadIMG;
import com.android.svmc.app.mobile.model.PostDetail;
import com.android.svmc.app.mobile.view.FontTextView;




public class ListPostInCategoryAdapter extends BaseAdapter{
	Context mcontext;
	private LayoutInflater mInflater;
	ArrayList<PostDetail> listpost = new ArrayList<PostDetail>();
	private boolean online;
	
	public ListPostInCategoryAdapter(Context context,
			ArrayList<PostDetail> posts) {
		mcontext = context;
		listpost = posts;
		mInflater = LayoutInflater.from(context);

	}
	public ListPostInCategoryAdapter(Context context,
			ArrayList<PostDetail> posts,boolean online) {
		mcontext = context;
		listpost = posts;
		mInflater = LayoutInflater.from(context);
		this.online = online;

	}
	@Override
	public int getCount() {
		
		return listpost.size();
	}

	@Override
	public Object getItem(int postion) {
		
		return listpost.get(postion);
	}

	@Override
	public long getItemId(int postion) {
		
		return postion;
	}

	@Override
	public View getView(int postion, View view, ViewGroup parent) {
		HolderPost holder;
		if(view==null){
			mInflater = (LayoutInflater) mcontext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = mInflater.inflate(R.layout.item_listpost_post, null);
			holder = new HolderPost();
			holder.thumbnail=(ImageView)view.findViewById(R.id.listpost_img_thumbnail);
			holder.title=(TextView)view.findViewById(R.id.listpost_txt_title);
			holder.author=(TextView)view.findViewById(R.id.listpost_txt_author);
			holder.datePublish=(TextView)view.findViewById(R.id.listpost_txt_timepost);
			view.setTag(holder);
		}else {
			holder =(HolderPost)view.getTag();
		}
		FontTextView.setfontMyriadPro_semiBold(holder.title, mcontext);
		FontTextView.setfontMyriadPro_regular(holder.author, mcontext);
		FontTextView.setfontMyriadPro_regular(holder.datePublish, mcontext);
		
		String imgUrl = listpost.get(postion).getThumbnail();
		if( imgUrl != null){
			if(online == true){
				DownloadIMG download = new DownloadIMG(imgUrl);
				download.Start(holder.thumbnail, this);	
				//Log.i("anhson", imgUrl);
			}else {
				holder.thumbnail.setImageResource(R.drawable.icon_default);
			}
		}else {
			holder.thumbnail.setImageResource(R.drawable.icon_default);
		}
	
		
	
		holder.title.setText(listpost.get(postion).getSubtitle());
		holder.author.setText(listpost.get(postion).getAuthor());
		holder.datePublish.setText(listpost.get(postion).getDatePublish());
		
		return view;
	}
	static class HolderPost{
		ImageView thumbnail;
		TextView title;
		TextView author;
		TextView datePublish;
	}

	

}
